<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/11/19
 * Time: 17:37
 */

namespace app\api\controller;


use language\Languagemsg;
use think\Db;

class Coupon extends Base
{
    private $user_id; // 用户ID
    private $page; // 页码
    private $create_time;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->msg_language = new Languagemsg();
        $this->language = input('language') ? input('language') : 0;
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id, $time, $value, $reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301, 'msg'=>$this->msg_language->otherLogin[$this->language]]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->msg_language->frost[$this->language]]);
            exit;
        }
    }
    
    /**
     * 优惠券详情
     * @return \think\response\Json
     * coupon_id      优惠券ID
     */
    public function couponDetail(){
        $coupon_id = input('coupon_id');
        $coupon = Db::table('up_coupon')
            ->where('coupon_id', $coupon_id)
            ->field('coupon_id, coupon_name, coupon_use_end_time, coupon_intro, coupon_type, 
            coupon_use_price, coupon_dikou_price, coupon_status, coupon_use_start_time')
            ->find();
        $is_get = Db::table('up_my_coupon')
            ->where('mc_coupon_id', $coupon_id)
            ->where('mc_user_id', $this->user_id)
            ->find();
        if($is_get){
            $coupon['is_get'] = 1; // 已经领取过
        }else{
            $coupon['is_get'] = 2; // 未领取过
        }
        return json(['status' => 200, 'msg' => $coupon]);
    }
    
    /**
     * 领取优惠券
     * @return \think\response\Json
     * coupon_id     优惠券ID
     */
    public function getCoupon(){
        $coupon_id = input('coupon_id');
        $coupon = Db::table('up_coupon')
            ->where('coupon_id', $coupon_id)
            ->field('coupon_use_price, coupon_dikou_price, coupon_type, coupon_total_num, coupon_lingqu_num, 
            coupon_use_start_time, coupon_use_end_time, coupon_status, coupon_id')
            ->find();
        // 优惠券状态
        if($coupon['coupon_status'] == 2){
//            return json(['status' => 0, 'msg' => '优惠券已下架']);
            return json(['status' => 0, 'msg' => $this->msg_language->couponDown[$this->language]]);
        }elseif($coupon['coupon_status'] == 3){
//            return json(['status' => 0, 'msg' => '优惠券已失效']);
            return json(['status' => 0, 'msg' => $this->msg_language->couponLose[$this->language]]);
        }
        // 过期
        $date = date('Y-m-d');
//        if($coupon['coupon_use_end_time'] < $date) return json(['status' => 0, 'msg' => '已过有效领取时间']);
        if($coupon['coupon_use_end_time'] < $date) return json(['status' => 0, 'msg' => $this->msg_language->couponOverTime[$this->language]]);
        // 领取数量
        if($coupon['coupon_total_num'] != 0){
            if($coupon['coupon_total_num'] <= $coupon['coupon_lingqu_num']){
//                return json(['status' => 0, 'msg' => '优惠券已领取完']);
                return json(['status' => 0, 'msg' => $this->msg_language->couponGetOver[$this->language]]);
            }
        }
        
        $my_coupon = [
            'mc_coupon_id' => $coupon_id,
            'mc_user_id' => $this->user_id,
            'mc_use_price' => $coupon['coupon_use_price'],
            'mc_dikou_price' => $coupon['coupon_dikou_price'],
            'mc_ling_time' => $this->create_time,
            'mc_use_start_time' => $coupon['coupon_use_start_time'],
            'mc_use_end_time' => $coupon['coupon_use_end_time']
        ];
        
        Db::startTrans();
        try{
            // 插入我的优惠券
            Db::table('up_my_coupon')->insert($my_coupon);
            // 已领取数量+1
            Db::table('up_coupon')->where('coupon_id', $coupon_id)->setInc('coupon_lingqu_num');
            Db::commit();
//            return json(['status' => 200, 'msg' => '领取成功']);
            return json(['status' => 200, 'msg' => $this->msg_language->getSuccess[$this->language]]);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }
}